package com.ccb.filter;

import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;

/**
 * 过滤工具类
 * @author flytoice
 *
 */
public class Filters {
	private static XMLConfiguration conf;
	static{
		try {
			conf = new XMLConfiguration("filter.xml");
			
			conf.setExpressionEngine(new XPathExpressionEngine());
		} catch (ConfigurationException e) {
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static RecordFilter getFilter(String projectName){
		if (projectName.equals(conf.getString("project[name='"+projectName+"']/name"))){
			RecordFilter filter = new RecordFilter();
			filter.setProjectName(projectName);
			
			List<String> o = conf.getList("project[name='"+projectName+"']/include");
			if (o.size()>0) filter.add2IncludeList(o);
			o = conf.getList("project[name='"+projectName+"']/exclude");
			if (o.size()>0) filter.add2ExcludeList(o);
			
			filter.resetUseFlag();
			return filter;
		}
		return null;
		
	}
	
	
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args){
		RecordFilter f;
		f = Filters.getFilter("atm");
		System.out.println(f.accept("201005"));	//true
		System.out.println(f.accept("201006"));	//false
		
		f = Filters.getFilter("pos");
		System.out.println(f.accept("8031"));	//true
		System.out.println(f.accept("d"));	//false
		
		f = Filters.getFilter("cjwy");
		System.out.println(f.accept("E"));	//true
		System.out.println(f.accept("a"));	//false
		
		f = Filters.getFilter("sjyh");
		System.out.println(f.accept("a"));	//true
		System.out.println(f.accept("G"));	//false
		
		f = Filters.getFilter("netbank");
		System.out.println(f.accept("a"));	//true
		System.out.println(f.accept("G"));	//true
		
		System.out.println("ddd异常");
		f = Filters.getFilter("ddd");
		System.out.println(f.accept("a"));
		
	}
}



